//
//  ViewController.m
//  14-半塘的晒单的下面
//
//  Created by mafeng on 16/8/2.
//  Copyright © 2016年 mafeng. All rights reserved.
//

#import "ViewController.h"
#import "ImageCollectionViewCell.h"
#import "LabelCollectionViewCell.h"
#import "LabelAndCollectionReusableView.m"
#define kCollectionViewHeaderCell @"header"
#define kCollectionView @"reuse"
#define kCollectionReusableViewHeader @"headerReuse"
#define kRandomColor [UIColor colorWithRed:arc4random() % 256 /255.0f green:arc4random() % 256 /255.0f blue:arc4random() % 256 /255.0f alpha:1]
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat width = (CGRectGetWidth(self.view.bounds) - 10) / 3;
    layout.itemSize = CGSizeMake(width, width);
    // 行间距
    layout.minimumLineSpacing = 5;
    // 列间距
    layout.minimumInteritemSpacing = 0;
    // 头大小
   // layout.headerReferenceSize = CGSizeMake(0, 50);
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    [collectionView registerClass:[ImageCollectionViewCell class] forCellWithReuseIdentifier:kCollectionView];
    
    [collectionView registerClass:[LabelCollectionViewCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionReusableViewHeader];
    
    // inset改变了bounds
    // 给collectview加头视图的三种方法
    // 1. 写section头(自定义头视图)
    // 2. 用collectionview的一个方块作为头
    // 3. 如下, 用scrollview的方法. 通过上边距实现视图添加
    collectionView.contentInset = UIEdgeInsetsMake(300, 0, 0, 0);
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -295, self.view.bounds.size.width, 290)];
    view.backgroundColor = [UIColor greenColor];
    [collectionView addSubview:view];
    
    [collectionView registerClass:[LabelAndCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionReusableViewHeader];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }
    return 33;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ImageCollectionViewCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionView forIndexPath:indexPath];
    cell.backgroundColor = kRandomColor;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(0, 300);
    }
    return CGSizeMake(0, 50);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
       LabelAndCollectionReusableView *cell =  [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionReusableViewHeader forIndexPath:indexPath];
        return cell;
    } else {
    LabelCollectionViewCell *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderCell forIndexPath:indexPath];
    reusableView.backgroundColor = [UIColor redColor];
    return reusableView;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
